﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class ProcessPackageRepository
    {
        // 添加工艺包
        public int AddProcessPackage(ProcessPackage processPackage)
        {
            string sql = @"INSERT INTO processpackage 
                            (Name, Description, CreationTime, UpdateTime) 
                           VALUES 
                            (@Name, @Description, @CreationTime, @UpdateTime)";

            MySqlParameter[] parameters = {
                new MySqlParameter("@Name", processPackage.Name),
                new MySqlParameter("@Description", processPackage.Description),
                new MySqlParameter("@CreationTime", processPackage.CreationTime),
                new MySqlParameter("@UpdateTime", processPackage.UpdateTime)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 删除工艺包
        public int DeleteProcessPackage(int processPackageId)
        {
            string sql = "DELETE FROM processpackage WHERE ProcessPackageId = @ProcessPackageId";
            MySqlParameter[] parameters = { new MySqlParameter("@ProcessPackageId", processPackageId) };
            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 更新工艺包信息
        public int UpdateProcessPackage(ProcessPackage processPackage)
        {
            string sql = @"UPDATE processpackage 
                            SET Name = @Name, 
                                Description = @Description, 
                                CreationTime = @CreationTime, 
                                UpdateTime = @UpdateTime 
                          WHERE ProcessPackageId = @ProcessPackageId";

            MySqlParameter[] parameters = {
                new MySqlParameter("@Name", processPackage.Name),
                new MySqlParameter("@Description", processPackage.Description),
                new MySqlParameter("@CreationTime", processPackage.CreationTime),
                new MySqlParameter("@UpdateTime", processPackage.UpdateTime),
                new MySqlParameter("@ProcessPackageId", processPackage.ProcessPackageId)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 查询所有工艺包
        public List<ProcessPackage> GetAllProcessPackages()
        {
            string sql = "SELECT * FROM processpackage";
            return DBHelper.ExecuteQuery(sql, null, MapProcessPackage);
        }

        // 查询单个工艺包
        public ProcessPackage GetProcessPackageById(int processPackageId)
        {
            string sql = "SELECT * FROM processpackage WHERE ProcessPackageId = @ProcessPackageId";
            MySqlParameter[] parameters = { new MySqlParameter("@ProcessPackageId", processPackageId) };
            var processPackages = DBHelper.ExecuteQuery(sql, parameters, MapProcessPackage);
            return processPackages.FirstOrDefault();
        }

        // 映射数据库查询结果到 ProcessPackage 对象
        private ProcessPackage MapProcessPackage(MySqlDataReader reader)
        {
            return new ProcessPackage
            {
                ProcessPackageId = (int)reader["ProcessPackageId"],
                Name = reader["Name"].ToString(),
                Description = reader["Description"] is DBNull ? null : reader["Description"].ToString(),
                CreationTime = (DateTime)reader["CreationTime"],
                UpdateTime = reader["UpdateTime"] is DBNull ? (DateTime?)null : (DateTime)reader["UpdateTime"]
            };
        }
    }
}